<?php
/**
 * Joomla! 1.5 component jhtraq
 *
 * @version $Id: view.html.php 2010-03-09 08:28:06 svn $
 * @author JHShop
 * @package Joomla
 * @subpackage jhtraq
 * @license GNU/GPL
 *
 * Traq type system for Joomla
 *
 * This component file was created using the Joomla Component Creator by Not Web Design
 * http://www.notwebdesign.com/joomla_component_creator/
 *
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

// Import Joomla! libraries
jimport( 'joomla.application.component.view');
class JhtraqViewReleases extends JView {
    function display($tpl = null) {

		$task = JRequest::getCmd('task', 'index');
		$tpl = $this->$task();
		
        parent::display($tpl);
    }
	
	function cancel()
	{
		$this->index();
	}
	
	function index()
	{
		$model =& JhtraqHelper::getModel('jhtraq');
		$this->releases =  $model->getReleases();
		$this->projects = $model->getProjectList();
		$this->versions = $model->getVersionList();
		$this->search = JRequest::getVar('search');
		$this->project = JRequest::getVar('project');
		$this->pagination = $model->getPagination();
		
		
	}
	
	function add()
	{
		$model =& JhtraqHelper::getModel('jhtraq');
		$this->release = $model->getRelease(0);
		$this->projects = $model->getProjectList();
		$this->versions = $model->getVersionList();
		$this->jhshopProds = $this->getJhshopProds();
                $this->ambraTypes = $this->getAmbrasubscriptions();
		return 'form';
	}
	
	function edit()
	{
		$model =& JhtraqHelper::getModel('jhtraq');
		$this->release = $model->getRelease($_POST['cid'][0]);
		$this->projects = $model->getProjectList();
		$this->versions = $model->getVersionList();
		$this->jhshopProds = $this->getJhshopProds();
                $this->ambraTypes = $this->getAmbrasubscriptions();
		return 'form';
	}
	
	function save()
	{
		$detail = $_POST;
		$model =& JhtraqHelper::getModel('jhtraq');
		$model->saveRelease($detail);
		$mainframe = JFactory::getApplication();
		$mainframe->redirect("index.php?option=com_jhtraq&view=releases", "Releases Updated");
	}
	
	
	function remove()
	{
		$model =& JhtraqHelper::getModel('releases');
		$model->remove($_POST['cid']);
		$mainframe = JFactory::getApplication();
		$mainframe->redirect("index.php?option=com_jhtraq&view=releases", "Releases Deleted");
	}
	
	//ajax funciton
	function vlist()
	{
		//print_r($_GET);
		$pid = (int)$_GET['pid'];
		$vid = (int)$_GET['vid'];
		$db =& JFactory::getDBO();
		$db->setQuery("Select * from #__jhtraq_versions where project_id='$pid'");
		$rows = $db->loadAssocList();
		//print_r($db);
		$html = '<select name="version_id" id="version_id">';
		foreach($rows as $row)
		{
			$s = ($row['id'] == $vid)?'Selected':'';
			$html .= '<option value="'.$row['id'].'" '.$s.'>'.$row['version'].'</option>';
		}
		$html .= '</select>';
		echo $html;
		//die();
		$mainframe = JFactory::getApplication();
		$mainframe->close();
	}
	
	function getJhshopProds()
	{
		$results = array();
		if(file_exists(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_jhshop' . DS . 'jhshop.php'))
		{
			
			$db =& JFactory::getDBO();
			//1 Get the default langauge for jhshop
			$db->setQuery("Select `value` from #__jhs_setting where `key`='config_language'");
			$langcode = $db->loadResult();
			$db->setQuery("select `language_id` from #__jhs_language where code = '$langcode'");
			$langid = $db->loadResult();
			$db->setQuery("select `product_id`,`name` from #__jhs_product_description where language_id='$langid' order by `name` asc");
			$rows = $db->loadAssocList();
			foreach($rows as $row)
			{
				$results[$row['product_id']] = $row['name'];
			}
		}
		return $results;
	}

        function getAmbrasubscriptions()
        {
     
            $results = array();
            if(file_exists(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_ambrasubs' . DS . 'ambrasubs.php'))
            {
            
                $db =&JFactory::getDBO();
                $db->setQuery("Select id, title from #__ambrasubs_types order by title asc ");
                $rows = $db->loadAssocList();
             
                foreach($rows as $row){
                    $results[$row['id']] = $row['title'];
                }
            }

            return $results;

        }

	
	
}
?>